home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / CD32 / CD32_Support / examples / SA_Examples / nonvolatile / StuffNV.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-31  |  6.0 KB  |  246 lines

  1. /* StuffNV.c
  2.  * 
  3.  * Purpose: fill NVRam as quickly as possible.
  4.  * Protect each item unless directed not to.
  5.  * 
  6.  */
  7.  
  8. /* Includes --------------------------------------------- */
  9. #include <exec/types.h>
  10. #include <exec/libraries.h>
  11. #include <exec/memory.h>
  12. #include <dos/dos.h>
  13. #include <dos/rdargs.h>
  14. #include <libraries/nonvolatile.h>
  15. #include <clib/exec_protos.h>
  16. #include <clib/dos_protos.h>
  17. #include <clib/nonvolatile_protos.h>
  18.  
  19. #include <stdlib.h>
  20. #include <string.h>
  21.  
  22. #include <pragmas/nonvolatile_pragmas.h>
  23.  
  24. /* Defines ------------------------------------------ */
  25. #define PROGNAME "StuffNV"
  26. #define ERRMSG_LIBNOOPEN "Couldn't open %s V%ld (or better)!\n"
  27. #define TEMPLATE "NAME/K,ITEMS/K/N,BYTES=BYTESPERITEM/K/N,NOPROTECT/S,OVERRUN/S"
  28. #define INAME_SIZE 80     /* size of itemName buffer */
  29. #define DEFAULT_APPNAME PROGNAME
  30. #define ITEM_ROOT_NAME "TestItem"
  31.  
  32. /* Structs ------------------------------ */
  33. struct Opts {
  34.     STRPTR name;            /* appnameroot to use */
  35.     LONG *items;            /* number of items */
  36.     LONG *bytes;            /* bytes per item */
  37.     LONG  noProtect;        /* don't protect? */
  38.     LONG  overrun;          /* try to store too much? */
  39. };
  40.  
  41. /* Protos ------------------------------------------ */
  42. static VOID GoodBye(int);
  43. static LONG doInit(VOID);
  44. extern VOID bsprintf(STRPTR,STRPTR,...);  /* errrpt.lib; sprintf() using RawDoFmt() */
  45. static VOID storeItem(STRPTR, ULONG, ULONG);
  46. static VOID protectItem(STRPTR, STRPTR);
  47. static ULONG findItemCount(ULONG);
  48.  
  49. /* Globals --------------------------------------- */
  50. struct Opts     opts;
  51. struct RDArgs  *rdargs;
  52. struct Library *NVBase;
  53. UBYTE  itemName[INAME_SIZE];
  54. UBYTE  appName[INAME_SIZE];
  55. UBYTE  tmpBuf[INAME_SIZE];
  56. int rc = RETURN_OK;
  57. ULONG  freeStore=10UL;
  58. STRPTR myData;
  59.  
  60. VOID main(int argc, UBYTE *argv[]) {
  61.     ULONG itemCount=0;
  62.     ULONG bytesPerItem;
  63.     BOOL doProtect=TRUE;
  64.     int i;
  65.  
  66.     if (!doInit()) {
  67.         GoodBye(RETURN_FAIL);
  68.     }
  69.  
  70.     myData = NULL;
  71.  
  72.     /* do we protect or not? */
  73.     if (opts.noProtect) {
  74.         doProtect = FALSE;
  75.     }
  76.  
  77.     /* use user's idea of a good name? */
  78.     if (opts.name) {
  79.         stccpy(appName, opts.name, INAME_SIZE-1);
  80.     }
  81.     else {
  82.         stccpy(appName, DEFAULT_APPNAME, INAME_SIZE-1);
  83.     }
  84.  
  85.     /* user's item count, or fill to order? */
  86.     if (opts.bytes) {
  87.         bytesPerItem = *opts.bytes;
  88.     }
  89.     else {
  90.         bytesPerItem = 10UL;
  91.     }
  92.  
  93.     if (opts.items) {
  94.         itemCount = *opts.items;
  95.     }
  96.     else {
  97.         itemCount = findItemCount(bytesPerItem);
  98.     }
  99.  
  100.     if (opts.overrun) {
  101.         itemCount += 2UL;
  102.     }
  103.  
  104.  
  105.     strcat(appName, "1");
  106.     for (i = 0; i < itemCount-2; i++) {
  107.         storeItem(appName, i, bytesPerItem);  /* changes itemName */
  108.         if (doProtect) {
  109.             protectItem(appName, itemName);
  110.         }
  111.         if (CheckSignal(SIGBREAKF_CTRL_C)) {
  112.             GoodBye(rc);
  113.         }
  114.     }
  115.  
  116.     appName[(strlen(appName)-1)] = '2';
  117.     storeItem(appName, 1, bytesPerItem);
  118.     if (doProtect) {
  119.         protectItem(appName, itemName);
  120.     }
  121.  
  122.     GoodBye(rc);
  123. }
  124. /* storeItem ====================================================
  125.    feed it appname, itemnumber, bytesperitem
  126.    If the buffers doesn't already exist, create it;
  127.    use existing one otherwise.  This is an ugly kludge, added
  128.    in solely for additional speed between calls to StoreNV().
  129.  
  130.    WE RELY ON "bytes" BEING THE SAME FROM CALL TO CALL!
  131.    (Yuck; should've written things differently in the 1st place...)
  132.  
  133. */
  134. static VOID storeItem(STRPTR aName, ULONG num, ULONG bytes) {
  135.     UWORD err;
  136.     ULONG roundedBytes=bytes, nvBytes10;
  137.  
  138.     bsprintf(itemName, "%s%ld", ITEM_ROOT_NAME, num);
  139.     if (bytes % 10UL) {
  140.         roundedBytes += (10UL - (bytes % 10UL));
  141.     }
  142.     nvBytes10 = roundedBytes / 10UL;
  143.  
  144.     if (!myData) {
  145.         if (NULL == (myData = AllocVec(roundedBytes, MEMF_ANY))) {
  146.             Printf("Couldn't allocate RAM for item %ld!\n", num);
  147.             return;
  148.         }
  149.     }
  150.  
  151.     memset(myData,(UBYTE)num, roundedBytes);
  152.     err = StoreNV(aName, itemName, myData, nvBytes10, FALSE);
  153.     Printf("StoreNV(%s,%s,%08lx,%lu) returned %lu\n", 
  154.             aName, itemName, myData, nvBytes10, (ULONG)err);
  155.     if (err) {
  156.         rc = RETURN_ERROR;
  157.     }
  158.  
  159.     return;
  160. }
  161.  
  162. /* protectItem ====================================================
  163.    feed it appname and itemname
  164. */
  165. static VOID protectItem(STRPTR aName, STRPTR iName) {
  166.     BOOL success;
  167.  
  168.     success = SetNVProtection(aName, itemName, NVEF_DELETE, FALSE);
  169.     Printf("Protect of %s returned %ld\n", itemName, (LONG)success);
  170.     if (!success) {
  171.         rc = RETURN_ERROR;
  172.     }
  173.     return;    
  174. }
  175.  
  176. /* findItemCount ===================================================
  177.    freeStore/bytesPerItem
  178.  */
  179. static ULONG findItemCount(ULONG bytes) {
  180.     struct NVInfo *ni;
  181.     ULONG itemcount;
  182.  
  183.     if (!bytes) {
  184.         PutStr("Bad, bad user.  ByteCount must be non-zero!\n");
  185.         GoodBye(RETURN_FAIL);
  186.     }
  187.  
  188.     if (ni = GetNVInfo(FALSE)) {
  189.         freeStore = ni->nvi_FreeStorage * 10UL;  /* we work in real bytes here :-) */
  190.         FreeNVData(ni);
  191.     }
  192.     else {
  193.         PutStr("Couldn't GetNVInfo()!\n");
  194.         GoodBye(RETURN_FAIL);
  195.     }
  196.     if (freeStore < 1UL) {
  197.         PutStr("Fewer than 10 bytes free!\n");
  198.         GoodBye(RETURN_FAIL);
  199.     }
  200.     itemcount = freeStore/bytes;
  201.  
  202.     return(itemcount);    
  203. }
  204.  
  205.  
  206. /* GoodBye ===============================================
  207.    Clean-exit routine.
  208.  */
  209. static VOID GoodBye(int rc) {
  210.  
  211.     if (myData) {
  212.         FreeVec(myData);
  213.     }
  214.  
  215.     if (NVBase) {
  216.         CloseLibrary(NVBase);
  217.     }
  218.  
  219.     if (rdargs) {
  220.         FreeArgs(rdargs);
  221.     }
  222.  
  223.     exit(rc);
  224. }
  225.  
  226. /* doInit =============================================
  227.  * Open libraries, call ReadArgs() if necessary.
  228.  * Returns TRUE for success, FALSE otherwise.
  229.  */
  230. static LONG doInit(VOID) {
  231.     rdargs = ReadArgs(TEMPLATE, (LONG *)&opts, NULL);
  232.     if (!rdargs) {
  233.         PrintFault(IoErr(), PROGNAME);
  234.         return(FALSE);
  235.     }
  236.  
  237.     NVBase = OpenLibrary("nonvolatile.library", 37L);
  238.     if (!NVBase) {
  239.         Printf(ERRMSG_LIBNOOPEN, "nonvolatile.library", 37L);
  240.         return(FALSE);
  241.     }
  242.  
  243.     return(TRUE);
  244. }
  245.  
  246.